package com.lq.memo.controller;

import com.google.gson.Gson;
import com.lq.memo.entity.MyResponse;
import com.lq.memo.entity.User;
import com.lq.memo.entity.WeChatModel;
import com.lq.memo.entity.WeChatSessionModel;
import com.lq.memo.exception.MyException;
import com.lq.memo.service.UserService;
import com.lq.memo.service.WchatLoginService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName WChatLoginContorller
 * @Description 微信登录
 * @Author LQ
 * @Date 2021/11/18 22:47
 * @Version 1.0
 */
@RequestMapping("/wchat")
@Slf4j
@Controller
public class WChatLoginContorller {

    @Autowired
    WchatLoginService wchatLoginService;

    @ResponseBody
    @RequestMapping("/login")
    public MyResponse login(@RequestBody WeChatModel weChatModel) {
        MyResponse myResponse = new MyResponse();
        try {
            WeChatSessionModel weChatSessionModel = wchatLoginService.wChatLogin(weChatModel);
            User user = wchatLoginService.myLogin(weChatSessionModel);
            return myResponse.success().data(user);
        } catch (MyException e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return myResponse.fail().message(e.toString());
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return myResponse.fail().message(e.toString());
        }
    }
}
